package com.yqyd.web.controller.hy.app;

import cn.hutool.core.date.DateUtil;
import com.yqyd.common.annotation.Anonymous;
import com.yqyd.common.core.controller.BaseController;
import com.yqyd.common.core.domain.R;
import com.yqyd.common.core.page.TableDataInfo;
import com.yqyd.common.utils.SecurityUtils;
import com.yqyd.common.utils.poi.ExcelUtil;
import com.yqyd.hy.domain.HyManifest;
import com.yqyd.hy.dto.AddManifestDto;
import com.yqyd.hy.dto.ManifestSearchDto;
import com.yqyd.hy.service.IHyManifestService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;


/**
 * 货单
 */
@RestController
@RequestMapping("/api/app/manifest")
@CrossOrigin
public class ApiManifestController extends BaseController {

    @Resource
    private IHyManifestService manifestService;


    @GetMapping("/List")
    public TableDataInfo List(ManifestSearchDto dto) {
        startPage();
        Long userId = SecurityUtils.getUserId();
        if(userId == 104L || userId == 107L || userId == 110L){
            userId = null;
        }
        return  getDataTable(manifestService.selectHyManifestList(new HyManifest().setUserId(userId).setName(dto.getKeyword()).setStatus(dto.getStatus())));
    }

    /**
     * 导出用户提交的货单列表
     */
    @PostMapping("/Export")
    public void export(HttpServletResponse response, ManifestSearchDto dto)
    {
        List<HyManifest> list = manifestService.selectHyManifestList(new HyManifest().setName(dto.getKeyword()).setStatus(dto.getStatus()));
        ExcelUtil<HyManifest> util = new ExcelUtil<HyManifest>(HyManifest.class);
        util.exportExcel(response, list, DateUtil.format(DateUtil.date(), "yyyy-MM-dd-") + "货单数据");
    }


    @PostMapping("/Add")
    public R<HyManifest> Add(@RequestBody @Valid AddManifestDto dto) {
        Long  userId = SecurityUtils.getUserId();
        manifestService.insertHyManifest(new HyManifest()
                .setUserId(userId)
                .setExpress(dto.getExpress())
                .setNumber(dto.getNumber())
                .setName(dto.getName())
                .setRemark(dto.getRemark()));
        return R.ok();
    }

    @GetMapping("Express/{number}")
    @Anonymous
    public R<Map<String,Object>> Express(@PathVariable("number") String number) {
        return R.ok(manifestService.SearchExpress(number));
    }

}
